import React from 'react';
import CommonList from '../../../components/CommonLists/list';
import { IS_SYSTEM } from '../../../utils/constant';
import { Button, message, Popconfirm } from 'antd';
import services from '../../../services/api';
import Modal from '../../../components/Modal/index';
import Relation from './relation/relation';

class FieldList extends CommonList {
  constructor(props) {
    super(props);
    this.api = 'fn';
    this.state = {
      applications: [],
      events: [],
    };
    this.operatorWidth = '80px';
    this.editButton = (record) => {
      return (
        <Button size='small' disabled={record.source === 1} type='link' onClick={this.versionManage.bind(this, record)}>
          编辑
        </Button>
      );
    };
    this.delButton = (record) => {
      return record.source === 1 ? (
        <Button disabled={record.source === 1} size='small' type='link'>
          删除
        </Button>
      ) : (
        <Popconfirm title='你确定要删除吗？' onConfirm={this.delete.bind(this, record)}>
          <Button size='small' type='link'>
            删除
          </Button>
        </Popconfirm>
      );
    };
    this.versionButton = (record) => {
      return (
        <Button size='small' type='link' onClick={this.versionManage.bind(this, record)}>
          版本管理
        </Button>
      );
    };
    this.showRelation = (record) => {
      return (
        <Button size='small' type='link' onClick={this.relation.bind(this, record)}>
          查看引用
        </Button>
      );
    };
    this.btnGroup = [this.editButton, this.delButton, this.versionButton, this.showRelation];
  }

  versionManage = (record) => {
    alert('待完成');
  };

  relation = (record) => {
    Modal.open({
      title: '查看引用',
      width: this.model_width ? this.model_width : '1000px',
      content: <Relation closeModal={this.closeModal} {...this.props} record={record} />,
      isOkBtn: false,
      maskClosable: false,
      isCancelBtn: false,
    });
  };

  async editPre(record) {
    const { code, data } = await services.fn.checkRef({ refCode: record.code });
    if (code === '0') {
      if (data > 0) {
        const index = Modal.open({
          title: '提醒',
          content: '该函数已被引用，编辑函数可能会影响使用',
          isOkBtn: true,
          maskClosable: false,
          isCancelBtn: true,
          ok: () => {
            Modal.close(index);
            this.edit(record, { title: '编辑函数' });
          },
        });
      } else {
        this.edit(record, { title: '编辑函数' });
      }
    }
  }
}

export default FieldList;
